package com.ifast.asset.controller;


import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import com.ifast.asset.domain.AntivirusDO;
import com.ifast.asset.domain.AntivirusdetailDO;
import com.ifast.asset.service.AntivirusService;
import com.ifast.asset.service.AntivirusdetailService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <pre>
 *
 * </pre>
 * <small> 2021-11-22 17:44:23 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/antivirus")
public class AntivirusController extends BaseController {
    @Autowired
    private AntivirusService antivirusService;
    @Autowired
    AntivirusdetailService antivirusdetailService;

    @ResponseBody
    @PostMapping("/list")
    public Result<Page<AntivirusDO>> list(@RequestBody AntivirusDO antivirusDTO) {
        Wrapper<AntivirusDO> wrapper = new EntityWrapper<>(antivirusDTO).orderBy("id", false);
        Page<AntivirusDO> page = antivirusService.selectPage(getPage(AntivirusDO.class, antivirusDTO.getPageNum(), antivirusDTO.getPageSize()), wrapper);
        return Result.ok(page);
    }

    @ResponseBody
    @PostMapping("/save")
    public Result<String> save(@RequestBody AntivirusDO antivirus) {
        antivirus.setTime(new Date());
        antivirusService.insert(antivirus);
        return Result.ok();
    }


    @PostMapping("/batchRemove")
    @ResponseBody
    public Result<String> remove(@RequestBody Integer[] ids) {
        antivirusService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    //获取详情
    @PostMapping("/getDetails")
    @ResponseBody
    public Result<Object> getDetails(@RequestBody AntivirusdetailDO antivirusdetailDO) {

        Wrapper wrapper = new EntityWrapper(antivirusdetailDO);
        Page<AntivirusdetailDO> page = antivirusdetailService.selectPage(getPage(AntivirusdetailDO.class, antivirusdetailDO.getPageNum(), antivirusdetailDO.getPageSize()), wrapper);
        return Result.ok(page);
    }

    @ResponseBody
    @GetMapping("/downLoadFile")
    public Result<Object> downLoadExcle(HttpServletResponse response, String id) {

    	AntivirusDO antivirusDO = antivirusService.selectById(id);
        response.setContentType("application/binary;charset=UTF-8");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
            ServletOutputStream out = response.getOutputStream();
            try {
                //设置文件头：最后一个参数是设置下载文件名(这里我们叫：张三.pdf)
                response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("antivirus" + ".xls", "UTF-8"));
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }

            String[] titles = {"ip","病毒路径", "病毒文件", "描述", "时间"};
            // 第一步，创建一个workbook，对应一个Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();

            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet hssfSheet = workbook.createSheet("sheet1");

            // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

            HSSFRow row = hssfSheet.createRow(0);
            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();

            //居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            HSSFCell hssfCell = null;
            for (int i = 0; i < titles.length; i++) {
                hssfCell = row.createCell(i);//列索引从0开始
                hssfCell.setCellValue(titles[i]);//列名1
                hssfCell.setCellStyle(hssfCellStyle);//列居中显示
            }

            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("antivirusid", id);
            List<AntivirusdetailDO> lists = antivirusdetailService.selectList(wrapper);

            for (int i = 0; i < lists.size(); i++) {
                row = hssfSheet.createRow(i + 1);
				row.createCell(0).setCellValue(antivirusDO.getName());
                row.createCell(1).setCellValue(lists.get(i).getUrl());
                row.createCell(2).setCellValue(lists.get(i).getFile());
                row.createCell(3).setCellValue(lists.get(i).getDescribe());
                row.createCell(4).setCellValue(simpleDateFormat.format(antivirusDO.getTime()));
            }

            // 第七步，将文件输出到客户端浏览器
            try {
                workbook.write(out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();


        }

        return Result.ok();
    }


}
